<template>
  <!-- 地区销售排行图表组件 -->
  <div class="com-container">
    <div class="com-chart" ref="rank"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      // echarts对象
      myChart: null,
      // 数据
      rankData: [],
      // 区域缩放的起点值
      startValue: 0,
      // 区域缩放的终点值
      endValue: 9,
      // 定时器
      timer: null
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.myChart.dispose()
      this.initChart()
      this.updateChart()
      this.screenAdapter()
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
    this.myChart.on('mouseover', () => {
      if (this.timer) clearInterval(this.timer)
    })
    this.myChart.on('mouseout', this.startInterval)
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    window.clearInterval(this.timer)
    this.myChart.off('mouseover')
    this.myChart.off('mouseout')
  },
  methods: {
    // 初始化echarts对象
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.rank, this.theme)
      const initOption = {
        title: {
          text: '▎地区销售排行',
          top: 20,
          left: 20
        },
        grid: {
          top: '40%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          show: true
        },
        dataZoom: {
          type: 'slider',
          show: true,
          startValue: this.startValue,
          endValue: this.endValue
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar',
            label: {
              show: true,
              position: 'top',
              color: '#fff'
            }
          }
        ]
      }
      this.myChart.setOption(initOption)
    },
    // 获取数据
    async getData() {
      // 处理数据后赋值给rankData
      const res = await this.$getApi.getRank()
      this.rankData = res.sort((a, b) => {
        return a.value - b.value
      })
      this.updateChart()
    },
    // 更新图表
    updateChart() {
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      // 省份数组
      const provinceArr = this.rankData.map(item => item.name)
      // 所以省份对应的销售金额
      const sellArr = this.rankData.map(item => item.value)
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        series: [
          {
            data: sellArr,
            itemStyle: {
              color: arg => {
                let targetColor
                if (arg.value > 300) {
                  targetColor = colorArr[0]
                } else if (arg.value > 200) {
                  targetColor = colorArr[1]
                } else {
                  targetColor = colorArr[2]
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: targetColor[0] },
                  { offset: 1, color: targetColor[1] }
                ])
              }
            }
          }
        ]
      }
      this.myChart.setOption(dataOption)
    },
    // 图表适配浏览器窗口大小变化
    screenAdapter() {
      const titleFontSize = (this.$refs.rank.offsetWidth / 100) * 3.6
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.myChart.setOption(adaptOption)
      this.myChart.resize()
    },
    // 定时器，每隔两秒向左移动一个
    startInterval() {
      if (this.timer) clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.startValue++
        this.endValue++
        if (this.endValue > this.rankData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        const changeOption = {
          dataZoom: {
            startValue: this.startValue,
            endValue: this.endValue
          }
        }
        this.myChart.setOption(changeOption)
      }, 2000)
    }
  }
}
</script>

<style>
</style>
